import ContextPadProvider from 'bpmn-js/lib/features/context-pad/ContextPadProvider'
import { assign } from 'min-dash'

type Injector = import('didi').Injector
type EventBus = import('diagram-js/lib/core/EventBus').default
type ContextPad = import('diagram-js/lib/features/context-pad/ContextPad').default
type Modeling = import('bpmn-js/lib/features/modeling/Modeling').default
type ElementFactory = import('bpmn-js/lib/features/modeling/ElementFactory').default
type AppendPreview = import('bpmn-js/lib/features/append-preview/AppendPreview').default
type Connect = import('diagram-js/lib/features/connect/Connect').default
type Create = import('diagram-js/lib/features/create/Create').default
type PopupMenu = import('diagram-js/lib/features/popup-menu/PopupMenu').default
type Rules = import('diagram-js/lib/features/rules/Rules').default
export type Translate = typeof import('diagram-js/lib/i18n/translate/translate').default
type Element = import('bpmn-js/lib/model/Types').Element
type Shape = import('bpmn-js/lib/model/Types').Shape

class EnhancementContextPadProvider extends ContextPadProvider {
  private _contextPad: ContextPad
  private _modeling: Modeling
  private _elementFactory: ElementFactory
  private _autoPlace: any
  private _connect: Connect
  private _create: Create
  private _popupMenu: PopupMenu
  private _canvas: any
  private _rules: Rules
  private _appendPreview: AppendPreview
  constructor(
    config: any,
    injector: Injector,
    eventBus: EventBus,
    contextPad: ContextPad,
    modeling: Modeling,
    elementFactory: ElementFactory,
    connect: Connect,
    create: Create,
    popupMenu: PopupMenu,
    canvas: any,
    rules: Rules,
    translate: Translate,
    appendPreview: AppendPreview,
  ) {
    super(
      config,
      injector,
      eventBus,
      contextPad,
      modeling,
      elementFactory,
      connect,
      create,
      popupMenu,
      canvas,
      rules,
      translate,
      appendPreview,
    )

    this._contextPad = contextPad
    this._modeling = modeling
    this._elementFactory = elementFactory
    this._connect = connect
    this._create = create
    this._popupMenu = popupMenu
    this._canvas = canvas
    this._rules = rules
    this._appendPreview = appendPreview

    this._autoPlace = injector.get('autoPlace', false)
  }

  getContextPadEntries(element: Element) {
    console.log('getContextPadEntries', element)
    const elementFactory = this._elementFactory,
      create = this._create,
      autoPlace = this._autoPlace,
      appendPreview = this._appendPreview
    const actions: Record<string, any> = {}

    function appendUserTask(event: Event, element: Shape) {
      const shape = elementFactory.createShape(assign({ type: 'bpmn:UserTask' }))
      create.start(event, shape, {
        source: element,
      })
    }

    const append = autoPlace
      ? function (_, element: Shape) {
          const shape = elementFactory.createShape(assign({ type: 'bpmn:UserTask' }))
          autoPlace.append(element, shape)
        }
      : appendUserTask

    const previewAppend = autoPlace
      ? function (_, element: Shape) {
          // mouseover
          appendPreview.create(element, 'bpmn:UserTask', {})
          return () => {
            // mouseout
            appendPreview.cleanUp()
          }
        }
      : null;
    if (element.type !== 'bpmn:Association' && element.type !== 'bpmn:TextAnnotation') {
      // 添加创建用户任务按钮
      actions['append.append-user-task'] = {
        group: 'model',
        className: 'bpmn-icon-user-task',
        title: '用户任务',
        action: {
          dragstart: appendUserTask,
          click: append,
          hover: previewAppend,
        },
      }
    }

    return actions
  }
}

export default EnhancementContextPadProvider
